﻿using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

namespace Sportsbook.CouchbasePersistence.JsonConverters
{
    public class JsonConverter<TInterface, TObject> : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(TInterface));
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return serializer.Deserialize<TObject>(reader);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            serializer.Serialize(writer, value);
        }
    }
    public class JsonListConverter<TInterface, TObject> : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(TInterface));
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return serializer.Deserialize<List<TObject>>(reader).Cast<TInterface>().ToList();
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            serializer.Serialize(writer, value);
        }
    }
}
